// 订单相关接口
import request from '@/utils/request'
import type {
  OrderPreResult,
  OrderCreateParams,
  OrderCreateResult,
  OrderResult,
  OrderLogisticResult,
  OrderListParams,
  OrderListResult
} from '@/types/order'

enum API {
  // 获取预付订单
  OrderPre_URL = '/member/order/pre',
  // 获取立即购买订单
  OrderNow_URL = '/member/order/pre/now',
  // 提交订单
  OrderSubmit_URL = '/member/order',
  // 通过订单 id 获取订单详情
  OrderById_URL = '/member/order/',
  // 获取再次购买订单
  OrderRepurchase_URL = '/member/order/repurchase/',
  // 模拟发货-内测版
  Consignment_URL = '/member/order/consignment/',
  // 删除订单
  OrderDel_URL = '/member/order',
  // 获取订单列表
  OrderListGer_URL = '/member/order'
}

/**
 * 获取预付订单
 */
export const reqGetOrderPre = () => {
  return request<OrderPreResult>({
    url: API.OrderPre_URL,
    method: 'GET'
  })
}

type OrderNowParams = {
  /** 商品 skuId */
  skuId: string
  /** 购买商品的数量 */
  count: string
  /** 下单时已经选择好的地址 id（可选）*/
  addressId?: string
}
/**
 * 获取立即购买订单
 * @param {OrderNowParams} data 请求参数
 */
export const reqGetOrderNow = (data: OrderNowParams) => {
  return request<OrderPreResult>({
    url: API.OrderNow_URL,
    method: 'GET',
    data
  })
}

/**
 * 提交订单
 * @param {OrderCreateParams} data 请求参数
 */
export const reqOrderSubmit = (data: OrderCreateParams) => {
  return request<OrderCreateResult>({
    url: API.OrderSubmit_URL,
    method: 'POST',
    data
  })
}

/**
 * 通过订单 id 获取订单详情
 * @param {string} id 订单 id
 */
export const reqOrderById = (id: string) => {
  return request<OrderResult>({
    url: API.OrderById_URL + id,
    method: 'GET'
  })
}

/**
 * 获取再次购买订单
 * @param id 订单 id
 */
export const reqOrderRepurchase = (id: string) => {
  return request<OrderPreResult>({
    url: API.OrderRepurchase_URL + id,
    method: 'GET'
  })
}

/**
 * 模拟发货-内测版
 * @param {string} id 订单 id
 */
export const reqConsignment = (id: string) => {
  return request({
    url: API.Consignment_URL + id,
    method: 'GET'
  })
}

/**
 * 确认收货
 * @param {string} id 订单 id
 */
export const reqOrderReceipt = (id: string) => {
  return request<{
    /** 订单状态，1为待付款、2为待发货、3为待收货、4为待评价、5为已完成、6为已取消 */
    orderState: number
  }>({
    url: `/member/order/${id}/receipt`,
    method: 'PUT'
  })
}

/**
 * 获取订单物流
 * @param {string} id  订单 id
 */
export const reqOrderLogistics = (id: string) => {
  return request<OrderLogisticResult>({
    url: `/member/order/${id}/logistics`,
    method: 'GET'
  })
}

/**
 * @description: 删除订单
 * @param {object} data 订单 id 列表
 * @return {*}
 */
export const reqOrderDel = (data: { ids: string[] }) => {
  return request({
    url: API.OrderDel_URL,
    method: 'DELETE',
    data
  })
}

/**
 * @description: 取消订单
 * @param {string} id 订单 id
 * @param {string} cancelReason 取消理由
 * @return {*}
 */
export const reqOrderCancel = (id: string, cancelReason: string) => {
  return request({
    url: `/member/order/${id}/cancel`,
    method: 'PUT',
    data: {
      cancelReason
    }
  })
}

//
// GET/member/order
/**
 * @description: 获取订单列表
 * @param {object} data 请求参数
 * @return {*}
 */
export const reqGetOrderList = (data: OrderListParams) => {
  return request<OrderListResult>({
    url: API.OrderListGer_URL,
    method: 'GET',
    data
  })
}
